<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="google-site-verification" content="TZE0rZyIqLl10trYu3BWBWa1Vmz6HFwhb2OcNEK4u-s" />
     <link rel="shortcut icon" href= /blog/img/favicon.ico >
    <title>
        cq’博客
    </title>
    <meta name="description" content= 嘿，我是程崎。欢迎访问！ >
    <meta name="keywords" content=  >
    
<link rel="stylesheet" href="/blog/libs/highlight/styles/monokai-sublime.css">

    
<link rel="stylesheet" href="/blog/libs/font-awesome/css/font-awesome.min.css">

    
<link rel="stylesheet" href="/blog/css/style.css">

<meta name="generator" content="Hexo 5.4.0"></head>
<body id="bodyx">
    <div class="hd posts">
    <a href="/blog/index.html"><i class="fa fa-home
 replay-btn" aria-hidden="true"></i></a>
    <div class="post-title">
        <p>
            docker相关
        </p>
        <hr>
    </div>
    <div class="post-content">
        <h1 id="Docker"><a href="#Docker" class="headerlink" title="Docker"></a>Docker</h1><p><img src="Docker.assets/image-20200606203315597.png" alt="image-20200606203315597"></p>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><p>官方文档：<a target="_blank" rel="noopener" href="https://docs.docker.com/docker-for-windows/">https://docs.docker.com/docker-for-windows/</a> </p>
<p>【官方文档超级详细】</p>
<p>仓库地址：<a target="_blank" rel="noopener" href="https://hub.docker.com/">https://hub.docker.com/</a></p>
<p>【发布到仓库，git pull push】</p>
<p>b站教程：<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1og4y1q7M4">https://www.bilibili.com/video/BV1og4y1q7M4</a>?</p>
<p>【这个教程非常简洁！且深入！基于企业应用场景！推荐！以下笔记都基于该课程】</p>
<h2 id="前期基础"><a href="#前期基础" class="headerlink" title="前期基础"></a>前期基础</h2><p>linux基本命令，类似cd，mkdir等</p>
<h2 id="Docker概述"><a href="#Docker概述" class="headerlink" title="Docker概述"></a>Docker概述</h2><h3 id="Docker为什么会出现"><a href="#Docker为什么会出现" class="headerlink" title="Docker为什么会出现"></a>Docker为什么会出现</h3><p>一款产品，开发和上线两套环境，应用环境配置费时费力，而且容易出问题</p>
<p>尤其对于机器学习和深度学习的库更是如此，很可能存在版本问题、底层依赖冲突问题</p>
<p>所以发布项目时，不只是一套代码过去，而是代码+环境整体打包过去</p>
<p>所谓开发即运维，保证系统稳定性，提高部署效率</p>
<p>使用Docker后的流程：</p>
<p>开发：建立模型–环境–打包带上环境，即镜像–放到Docker仓库</p>
<p>部署：下载Docker中的镜像，直接运行即可</p>
<p>Docker的思想来自于集装箱，集装箱，对环境进行隔离</p>
<p>Docker通过隔离机制，可以将服务器利用到极致。</p>
<h3 id="Docker的历史"><a href="#Docker的历史" class="headerlink" title="Docker的历史"></a>Docker的历史</h3><p>2010年，几个搞IT的人，在美国成立一家公司<code>dotCloud</code></p>
<p>做一些pass的云计算服务</p>
<p>他们将自己的容器化技术命名为Docker</p>
<p>Docker基于Go语言开发</p>
<p>Docker刚刚诞生的时候，没有引起行业的注意，dotCloud活不下去</p>
<p>然后他们决定开源</p>
<p>2013年，创始人将Docker开源，不开则以，一开惊人，刚开源的时候，每个月都会更新一个版本</p>
<p>2014年4月9日，Docker 1.0发布</p>
<h3 id="容器vs虚拟机"><a href="#容器vs虚拟机" class="headerlink" title="容器vs虚拟机"></a>容器vs虚拟机</h3><p>在容器技术出来之前，用的是虚拟机技术</p>
<h4 id="虚拟机原理示意图"><a href="#虚拟机原理示意图" class="headerlink" title="虚拟机原理示意图"></a>虚拟机原理示意图</h4><p><img src="Docker.assets/image-20200606205436434.png" alt="image-20200606205436434"></p>
<p>缺点：</p>
<ol>
<li>资源占用多</li>
<li>冗余步骤多</li>
<li>启动很慢</li>
</ol>
<h4 id="容器化技术示意图"><a href="#容器化技术示意图" class="headerlink" title="容器化技术示意图"></a>容器化技术示意图</h4><p>不是模拟的完整的操作系统</p>
<p><img src="Docker.assets/image-20200606205739655.png" alt="image-20200606205739655"></p>
<h4 id="二者对比"><a href="#二者对比" class="headerlink" title="二者对比"></a>二者对比</h4><p>比较虚拟机和Docker的不同</p>
<table>
<thead>
<tr>
<th></th>
<th>传统虚拟机</th>
<th>Docker</th>
</tr>
</thead>
<tbody><tr>
<td>虚拟内容</td>
<td>硬件+完整的操作系统+软件</td>
<td>APP+LIB</td>
</tr>
<tr>
<td>大小</td>
<td>笨重，通常几个G</td>
<td>轻便几个M或KB</td>
</tr>
<tr>
<td>启动速度</td>
<td>慢，分钟级</td>
<td>快，秒级</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody></table>
<h2 id="Docker安装"><a href="#Docker安装" class="headerlink" title="Docker安装"></a>Docker安装</h2><h3 id="Docker的基本组成"><a href="#Docker的基本组成" class="headerlink" title="Docker的基本组成"></a>Docker的基本组成</h3><p><img src="Docker.assets/image-20200606212250845.png" alt="image-20200606212250845"></p>
<p>明确几个概念：</p>
<ol>
<li><p>镜像(image)：docker镜像好比一个模板，可以通过这个模板来创建容器(container)，一个镜像可以创建多个容器，类似Python中的Class</p>
</li>
<li><p>容器(container)：类似Python中通过Class创建的实例，Object；容器可以理解为一个简易的系统</p>
</li>
<li><p>仓库(repository)：存放镜像的地方，</p>
<p>分为共有仓库和私有仓库</p>
<ul>
<li><p>Docker Hub：国外的</p>
</li>
<li><p>阿里云：配置镜像加速</p>
</li>
</ul>
</li>
</ol>
<h3 id="环境准备"><a href="#环境准备" class="headerlink" title="环境准备"></a>环境准备</h3><p>我们要有一台服务器，并且可以操作它</p>
<ol>
<li>Linux命令基础，购买linux阿里云的服务器</li>
<li>CentOS 7</li>
<li>使用Xshell链接远程服务器</li>
</ol>
<h3 id="安装xshell"><a href="#安装xshell" class="headerlink" title="安装xshell"></a>安装xshell</h3><p>下载CentOS7 <a target="_blank" rel="noopener" href="https://www.jianshu.com/p/a63f47e096e8">https://www.jianshu.com/p/a63f47e096e8</a></p>
<p>下载VMware 360软件管家下载</p>
<p>VMware配置虚拟机 <a target="_blank" rel="noopener" href="https://blog.csdn.net/babyxue/article/details/80970526">https://blog.csdn.net/babyxue/article/details/80970526</a></p>
<p>xshell链接服务器 <a target="_blank" rel="noopener" href="https://blog.csdn.net/zzy1078689276/article/details/77280814">https://blog.csdn.net/zzy1078689276/article/details/77280814</a></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[root@192 ~]# cd /</span><br><span class="line">[root@192 /]# pwd</span><br><span class="line">/</span><br><span class="line">[root@192 /]# ls</span><br><span class="line">bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var</span><br><span class="line">[root@192 /]# uname -r</span><br><span class="line">3.10.0-1127.el7.x86_64</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200608091950655.png" alt="image-20200608091950655"></p>
<p>用户名 </p>
<p>密码</p>
<p>查看ip：vmware里面输入，ip addIPr</p>
<p>视频书签，p602:20</p>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1og4y1q7M4?p=6">https://www.bilibili.com/video/BV1og4y1q7M4?p=6</a></p>
<h3 id="Centos安装"><a href="#Centos安装" class="headerlink" title="Centos安装"></a>Centos安装</h3><p><a target="_blank" rel="noopener" href="https://docs.docker.com/engine/install/centos/">https://docs.docker.com/engine/install/centos/</a></p>
<h3 id="卸载旧的版本"><a href="#卸载旧的版本" class="headerlink" title="卸载旧的版本"></a>卸载旧的版本</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># 卸载旧的版本</span><br><span class="line">$ sudo yum remove docker \</span><br><span class="line">                  docker-client \</span><br><span class="line">                  docker-client-latest \</span><br><span class="line">                  docker-common \</span><br><span class="line">                  docker-latest \</span><br><span class="line">                  docker-latest-logrotate \</span><br><span class="line">                  docker-logrotate \</span><br><span class="line">                  docker-engine</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200608092628498.png" alt="image-20200608092628498"></p>
<h3 id="安装基本环境"><a href="#安装基本环境" class="headerlink" title="安装基本环境"></a>安装基本环境</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 安装基本的安装包</span><br><span class="line">$ sudo yum install -y yum-utils</span><br></pre></td></tr></table></figure>

<p>!</p>
<p><img src="Docker.assets/image-20200608093114774.png" alt="image-20200608093114774"></p>
<h3 id="设置镜像的仓库"><a href="#设置镜像的仓库" class="headerlink" title="设置镜像的仓库"></a>设置镜像的仓库</h3><p>注意！！下载默认用国外的，太慢不要用！</p>
<p>用国内镜像，百度搜索，docker的阿里云镜像地址</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># 不要用官网默认这个！</span><br><span class="line">$ sudo yum-config-manager \</span><br><span class="line">    --add-repo \</span><br><span class="line">    https://download.docker.com/linux/centos/docker-ce.repo # 默认是国外的</span><br><span class="line"></span><br><span class="line"># 换成下面的</span><br><span class="line"></span><br><span class="line">$ sudo yum-config-manager \</span><br><span class="line">    --add-repo \</span><br><span class="line">    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 阿里云镜像</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200616145430166.png" alt="image-20200616145430166"></p>
<p>直接复制粘贴就OK了</p>
<p>更像软件包索引</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum makecache fast</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200616150014082.png" alt="image-20200616150014082"></p>
<p>没有问题的话就是可以用的</p>
<h3 id="安装docker引擎"><a href="#安装docker引擎" class="headerlink" title="安装docker引擎"></a>安装docker引擎</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install docker-ce docker-ce-cli containerd.io <span class="comment"># docker-ce 社区版 ee 企业版</span></span><br></pre></td></tr></table></figure>

<p>注意这里会有几个个y/n的判断</p>
<p><img src="Docker.assets/image-20200616150818860.png" alt="image-20200616150818860"></p>
<p><img src="Docker.assets/image-20200616150922549.png" alt="image-20200616150922549"></p>
<p>要看到Complet再收手！</p>
<h3 id="启动Docker"><a href="#启动Docker" class="headerlink" title="启动Docker"></a>启动Docker</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl start docker # 代表启动成功</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker version</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">Client: Docker Engine - Community</span><br><span class="line"> Version:           19.03.11</span><br><span class="line"> API version:       1.40</span><br><span class="line"> Go version:        go1.13.10</span><br><span class="line"> Git commit:        42e35e61f3</span><br><span class="line"> Built:             Mon Jun  1 09:13:48 2020</span><br><span class="line"> OS/Arch:           linux/amd64</span><br><span class="line"> Experimental:      false</span><br><span class="line"></span><br><span class="line">Server: Docker Engine - Community</span><br><span class="line"> Engine:</span><br><span class="line">  Version:          19.03.11</span><br><span class="line">  API version:      1.40 (minimum version 1.12)</span><br><span class="line">  Go version:       go1.13.10</span><br><span class="line">  Git commit:       42e35e61f3</span><br><span class="line">  Built:            Mon Jun  1 09:12:26 2020</span><br><span class="line">  OS/Arch:          linux/amd64</span><br><span class="line">  Experimental:     false</span><br><span class="line"> containerd:</span><br><span class="line">  Version:          1.2.13</span><br><span class="line">  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429</span><br><span class="line"> runc:</span><br><span class="line">  Version:          1.0.0-rc10</span><br><span class="line">  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd</span><br><span class="line"> docker-init:</span><br><span class="line">  Version:          0.18.0</span><br><span class="line">  GitCommit:        fec3683</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run hello-world</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200616151641013.png" alt="image-20200616151641013"></p>
<p>中间一堆是签名信息</p>
<p>run的运行流程图</p>
<p><img src="Docker.assets/image-20200616161441669.png" alt="image-20200616161441669"></p>
<p>查看下载的镜像</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker images</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200616151913277.png" alt="image-20200616151913277"></p>
<h3 id="卸载Docker"><a href="#卸载Docker" class="headerlink" title="卸载Docker"></a>卸载Docker</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># 卸载依赖yum remove docker-ce docker-ce-cli containerd.io# 删除资源rm -rf /var/lib/docker # docker 的默认工作路径</span><br></pre></td></tr></table></figure>

<h3 id="阿里云镜像加速"><a href="#阿里云镜像加速" class="headerlink" title="阿里云镜像加速"></a>阿里云镜像加速</h3><p>支付宝扫码登录，短信验证，确认授权</p>
<p>我有两个问题</p>
<ol>
<li><p>阿里云镜像加速必须配置嘛？</p>
<blockquote>
<p>哎呦我去: 加速快</p>
<p>哎呦我去: 要是翻墙就无所谓了</p>
</blockquote>
</li>
<li><p>这个阿里云必须要买嘛，买哪个，我看阿里云好多产品</p>
<blockquote>
<p>哎呦我去:不需要买</p>
<p>哎呦我去:免费的</p>
<p>哎呦我去:阿里云搜索容器服务</p>
<p>哎呦我去:有一个镜像加速</p>
</blockquote>
</li>
<li><p>这个地址是哪来的呀</p>
</li>
</ol>
<p>操作</p>
<p><img src="Docker.assets/image-20200616154429105.png" alt="image-20200616154429105"></p>
<p><img src="Docker.assets/image-20200616154455964.png" alt="image-20200616154455964"></p>
<p>控制台搜索 容器镜像服务</p>
<p><img src="Docker.assets/image-20200616155201285.png" alt="image-20200616155201285"></p>
<p>找到加速地址</p>
<p><img src="Docker.assets/image-20200616155649476.png" alt="image-20200616155649476"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo mkdir -p /etc/docker # 创建一个陌路sudo tee /etc/docker/daemon.json &lt;&lt;-&#x27;EOF&#x27;&#123;  &quot;registry-mirrors&quot;: [&quot;https://uyfgafsw.mirror.aliyuncs.com&quot;]&#125;EOF # 编写配置文件sudo systemctl daemon-reload # 重启服务sudo systemctl restart docker # 重启docker</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200616160315298.png" alt="image-20200616160315298"></p>
<p><a target="_blank" rel="noopener" href="https://www.processon.com/">在线画图软件ProcessOn</a></p>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV177411K7bH">服务器及购买相关介绍</a></p>
<h2 id="底层原理"><a href="#底层原理" class="headerlink" title="底层原理"></a>底层原理</h2><p>Docker是真么工作的？</p>
<p>Docker是一个Client-Server结构的系统，Docker的守护进程在主机上。通过Socket从客户端访问！</p>
<p>DockerServer接受到Docker-Client的指令，</p>
<p><img src="Docker.assets/image-20200616162107363.png" alt="image-20200616162107363"></p>
<p>Docker为什么比VM快？</p>
<ol>
<li>Docker有着比虚拟机更少的抽象层</li>
<li>docker主要用的是宿主机的内核，vm需要Guest OS</li>
</ol>
<p><img src="Docker.assets/image-20200616162302653.png" alt="image-20200616162302653"></p>
<p>所以说新建一个容器的时候，docker不需要像虚拟机一样重新加载一个操作系统内核，避免引导</p>
<h2 id="Docker命令"><a href="#Docker命令" class="headerlink" title="Docker命令"></a>Docker命令</h2><h3 id="帮助命令"><a href="#帮助命令" class="headerlink" title="帮助命令"></a>帮助命令</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker version # 显示docker的基本信息docker info # 系统信息，镜像和容器的数量docker 命令 --help # 全部信息</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://docs.docker.com/reference/">官网文档</a></p>
<p><img src="Docker.assets/image-20200616163338187.png" alt="image-20200616163338187"></p>
<h3 id="镜像命令"><a href="#镜像命令" class="headerlink" title="镜像命令"></a>镜像命令</h3><h4 id="docker-images"><a href="#docker-images" class="headerlink" title="docker images"></a>docker images</h4><p>查看所有本地主机上的镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@192 ~]<span class="comment"># docker images</span></span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200616172056530.png" alt="image-20200616172056530"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># 解释REPOSITORY  # 镜像仓库源TAG                 # 镜像的标签IMAGE ID           # 镜像的IDCREATED           # 镜像的创建时间SIZE # 镜像的大小</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">--all , -a		Show all images (default hides intermediate images) <span class="comment"># 显示所有--digests		Show digests--filter , -f		Filter output based on conditions provided--format		Pretty-print images using a Go template--no-trunc		Don’t truncate output--quiet , -q		Only show numeric IDs # 只显示id</span></span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200616172651835.png" alt="image-20200616172651835"></p>
<h4 id="docker-search"><a href="#docker-search" class="headerlink" title="docker search"></a>docker search</h4><p>搜索仓库中的镜像，相当于网页搜索</p>
<p><a target="_blank" rel="noopener" href="http://hub.docker.com/">网页搜索</a></p>
<p><img src="Docker.assets/image-20200616173009473.png" alt="image-20200616173009473"></p>
<p>mysql</p>
<p><img src="Docker.assets/image-20200616173050756.png" alt="image-20200616173050756"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker search mysql</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200616173308194.png" alt="image-20200616173308194"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker search --help</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200616173740981.png" alt="image-20200616173740981"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 解释Options:  -f, --filter filter   Filter output based on conditions provided      --format string   Pretty-print search using a Go template      --limit int       Max number of search results (default 25)      --no-trunc        Don&#x27;t truncate output</span></span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker search mysql --filter=STARS=3000 # 搜索出Stars大于3000的</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200616174440284.png" alt="image-20200616174440284"></p>
<p>【视频书签，<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1og4y1q7M4?p=9%EF%BC%8C13:44%EF%BC%8C%E5%A4%AA%E7%83%AD%E4%BA%86%E6%88%91%E8%A6%81%E7%BC%93%E4%B8%80%E7%BC%93%E3%80%91">https://www.bilibili.com/video/BV1og4y1q7M4?p=9，13:44，太热了我要缓一缓】</a></p>
<p>【20200616拳击课回来，好爽，赶紧把这个命令熟悉完】</p>
<h4 id="docker-pull"><a href="#docker-pull" class="headerlink" title="docker pull"></a>docker pull</h4><p>下载镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull mysql <span class="comment"># 下载mysql镜像，default tag，默认最新版latest</span></span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">[root@192 ~]<span class="comment"># sudo systemctl daemon-reload</span></span><br><span class="line">[root@192 ~]<span class="comment"># sudo systemctl restart docker</span></span><br><span class="line">[root@192 ~]<span class="comment"># docker pull mysql</span></span><br><span class="line">Using default tag: latest <span class="comment"># 不写tag默认最新版</span></span><br><span class="line">latest: Pulling from library/mysql</span><br><span class="line">8559a31e96f4: Pull complete  <span class="comment"># layer 分层下载，docker image的核心 联合文件系统</span></span><br><span class="line">d51ce1c2e575: Pull complete </span><br><span class="line">c2344adc4858: Pull complete </span><br><span class="line">fcf3ceff18fc: Pull complete </span><br><span class="line">16da0c38dc5b: Pull complete </span><br><span class="line">b905d1797e97: Pull complete </span><br><span class="line">4b50d1c6b05c: Pull complete </span><br><span class="line">c75914a65ca2: Pull complete </span><br><span class="line">1ae8042bdd09: Pull complete </span><br><span class="line">453ac13c00a3: Pull complete </span><br><span class="line">9e680cd72f08: Pull complete </span><br><span class="line">a6b5dc864b6c: Pull complete </span><br><span class="line">Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6<span class="comment">#签名</span></span><br><span class="line">Status: Downloaded newer image <span class="keyword">for</span> mysql:latest</span><br><span class="line">docker.io/library/mysql:latest <span class="comment"># 真实地址</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 即</span></span><br><span class="line">docker pull mysql</span><br><span class="line"><span class="comment"># 等价于</span></span><br><span class="line">docker pull docker.io/library/mysql:latest</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 指定版本下载 </span></span><br><span class="line">docker pull mysql:5.7</span><br></pre></td></tr></table></figure>

<p>版本来自于官网，版本库<a target="_blank" rel="noopener" href="https://hub.docker.com/_/mysql">https://hub.docker.com/_/mysql</a></p>
<p><img src="Docker.assets/image-20200617094339687.png" alt="image-20200617094339687"></p>
<p><img src="Docker.assets/image-20200617100948088.png" alt="image-20200617100948088"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker images</span><br></pre></td></tr></table></figure>

<p>此时查看镜像，可以看到新下载的两个</p>
<p><img src="Docker.assets/image-20200617101105899.png" alt="image-20200617101105899"></p>
<h4 id="docker-rmi"><a href="#docker-rmi" class="headerlink" title="docker rmi"></a>docker rmi</h4><p>remove images</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 删除一个 可以通过名称 也可以指定id -f表示删除所有docker rmi -f 9cfcce23593a# 删除多个 用空格分隔iddocker rmi -f id id id# 删除所有 docker rmi -f $(docker images -aq) # images -aq就是查所有镜像id，从而递归删除</span></span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200617102049613.png" alt="image-20200617102049613"></p>
<p><img src="Docker.assets/image-20200617102126526.png" alt="image-20200617102126526"></p>
<h3 id="容器命令"><a href="#容器命令" class="headerlink" title="容器命令"></a>容器命令</h3><p>说明：有了镜像才能创建容器，linux，下载一个centos镜像来测试学习</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull centos</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200617103406974.png" alt="image-20200617103406974"></p>
<h4 id="新建容器并启动"><a href="#新建容器并启动" class="headerlink" title="新建容器并启动"></a>新建容器并启动</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">docker run [可选参数] image</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 参数说明</span></span><br><span class="line">--name=“Name” # 容器名字，用于区分容器</span><br><span class="line">-d 后台方式运行</span><br><span class="line">-it 使用交互方式运行，进入容器查看内容</span><br><span class="line">-p 指定容器的端口 如-p 8080::8080</span><br><span class="line">	-p ip:主机端口：容器端口</span><br><span class="line">	-p 主机端口:容器端口</span><br><span class="line">	-p 容器端口</span><br><span class="line">	</span><br><span class="line">-p 随机指定端口</span><br></pre></td></tr></table></figure>

<h4 id="进入退出容器"><a href="#进入退出容器" class="headerlink" title="进入退出容器"></a>进入退出容器</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 进入</span></span><br><span class="line">docker run -it centos /bin/bash </span><br><span class="line"><span class="comment"># 查看目录</span></span><br><span class="line">ls</span><br><span class="line"><span class="comment"># 退出</span></span><br><span class="line"><span class="built_in">exit</span></span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200617104004004.png" alt="image-20200617104004004"></p>
<p>注意这里面主机名，编程了centos的id</p>
<p>这里面就是一个容器，套娃啊</p>
<h4 id="查看运行的容器"><a href="#查看运行的容器" class="headerlink" title="查看运行的容器"></a>查看运行的容器</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"># 查看正在运行的容器docker ps# 查看曾经运行的容器docker ps -a# 显示最近创建的容器，设置显示个数docker ps -a - n=? # 只显示容器的编号docker ps -aq</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@192 ~]# docker psCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES[root@192 ~]# docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES9939864fa2e6        centos              &quot;bin/bash&quot;          4 minutes ago       Exited (0) 4 minutes ago                       unruffled_knuth5f42e9930435        centos              &quot;/bin/bash&quot;         8 minutes ago       Exited (0) 4 minutes ago                       lucid_cannona89ddb393d3d        bf756fb1ae65        &quot;/hello&quot;            19 hours ago        Exited (0) 19 hours ago                        gracious_bhabha[root@192 ~]# docker ps -a -n=2CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES9939864fa2e6        centos              &quot;bin/bash&quot;          6 minutes ago       Exited (0) 6 minutes ago                       unruffled_knuth5f42e9930435        centos              &quot;/bin/bash&quot;         10 minutes ago      Exited (0) 7 minutes ago [root@192 ~]# docker ps -aq9939864fa2e65f42e9930435a89ddb393d3d</span><br></pre></td></tr></table></figure>

<h4 id="退出容器"><a href="#退出容器" class="headerlink" title="退出容器"></a>退出容器</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 容器停止退出<span class="built_in">exit</span><span class="comment"># 容器不停止退出 注意必须在英文输入法下，中文输入法不行Ctrl + P + Q</span></span></span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@192 ~]# docker run -it centos /bin/bash[root@bfcea13c40cd /]# [root@192 ~]# docker ps ##注意这里会自动给个命令，删掉CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMESbfcea13c40cd        centos              &quot;/bin/bash&quot;         About a minute ago   Up About a minute                       stoic_wilsonedbd9366d959        centos              &quot;/bin/bash&quot;         7 minutes ago        Up 7 minutes                            affectionate_bartik[root@192 ~]# docker exec -it edbd9366d959 /bin/bash ## 再次进入[root@edbd9366d959 /]# exit ##停止并推出exit</span><br></pre></td></tr></table></figure>

<h4 id="删除容器"><a href="#删除容器" class="headerlink" title="删除容器"></a>删除容器</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 删除指定容器 不能删除正在运行的容器，如果强制删除 rm -fdocker rm 容器id<span class="comment"># 删除所有容器docker rm -f $(docker ps -aq)# 删除所有容器docker ps -a -q|xargs docker rm</span></span></span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@192 ~]# docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMESbfcea13c40cd        centos              &quot;/bin/bash&quot;         29 minutes ago      Up 29 minutes                                   stoic_wilsonedbd9366d959        centos              &quot;/bin/bash&quot;         35 minutes ago      Up 35 minutes                                   affectionate_bartik9939864fa2e6        centos              &quot;bin/bash&quot;          48 minutes ago      Exited (0) 48 minutes ago                       unruffled_knuth5f42e9930435        centos              &quot;/bin/bash&quot;         52 minutes ago      Exited (0) 49 minutes ago                       lucid_cannona89ddb393d3d        bf756fb1ae65        &quot;/hello&quot;            20 hours ago        Exited (0) 20 hours ago                         gracious_bhabha[root@192 ~]# docker rm 5f42e99304355f42e9930435[root@192 ~]# docker rm edbd9366d959      # 注意正在运行的容器不能删除Error response from daemon: You cannot remove a running container edbd9366d9596c744dd449119269b04de2f2a494e7fc471f6396bcefd94c33fe. Stop the container before attempting removal or force remove</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@192 ~]# docker ps -aq # 所有容器idbfcea13c40cdedbd9366d9599939864fa2e6a89ddb393d3d[root@192 ~]# docker rm -f $(docker ps -aq) # 全部删除bfcea13c40cdedbd9366d9599939864fa2e6a89ddb393d3d</span><br></pre></td></tr></table></figure>

<h4 id="启动和停止容器的操作"><a href="#启动和停止容器的操作" class="headerlink" title="启动和停止容器的操作"></a>启动和停止容器的操作</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker startdocker restartdocker stopdocker kill</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">[root@192 ~]# docker run -it centos /bin/bash</span><br><span class="line">[root@7b1a7dd10ea4 /]# exit</span><br><span class="line">exit</span><br><span class="line">[root@192 ~]# docker ps #查看正在运行的</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES</span><br><span class="line">[root@192 ~]# docker ps -a # 查看历史运行过的</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES</span><br><span class="line">7b1a7dd10ea4        centos              &quot;/bin/bash&quot;         54 seconds ago      Exited (0) 42 seconds ago                       fervent_mirzakhani</span><br><span class="line">[root@192 ~]# docker start 7b1a7dd10ea4 # 启动当前这个容器 container id 粘过 来</span><br><span class="line">7b1a7dd10ea4</span><br><span class="line">[root@192 ~]# docker ps # 查看当前运行容器 发现启动成功</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES</span><br><span class="line">7b1a7dd10ea4        centos              &quot;/bin/bash&quot;         2 minutes ago       Up 28 seconds                           fervent_mirzakhani</span><br><span class="line">[root@192 ~]# docker stop 7b1a7dd10ea4 # 停止运行</span><br><span class="line">7b1a7dd10ea4</span><br><span class="line">[root@192 ~]# docker ps # 再次查看 没有这个容器了</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="常用其他命令"><a href="#常用其他命令" class="headerlink" title="常用其他命令"></a>常用其他命令</h3><p>【视频书签：<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1og4y1q7M4?p=11%EF%BC%8C%E4%B8%8D%E7%9F%A5%E4%B8%8D%E8%A7%89%E6%90%9E%E4%BA%86%E4%B8%80%E4%B8%8A%E5%8D%88%E4%BA%86%EF%BC%8C%E6%8C%89%E9%83%A8%E5%B0%B1%E7%8F%AD%E5%AD%A6%E4%B8%9C%E8%A5%BF%E7%9A%84%E6%84%9F%E8%A7%89%E7%9C%9F%E5%A5%BD%EF%BC%8C%E5%BE%97%E6%89%BE%E6%97%B6%E9%97%B4%E5%BF%AB%E7%82%B9%E5%88%B7%E5%AE%8C%E3%80%91">https://www.bilibili.com/video/BV1og4y1q7M4?p=11，不知不觉搞了一上午了，按部就班学东西的感觉真好，得找时间快点刷完】</a></p>
<h4 id="后台启动docker"><a href="#后台启动docker" class="headerlink" title="后台启动docker"></a>后台启动docker</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker run -d 镜像名</span><br><span class="line"><span class="meta">#</span><span class="bash"> 用docker ps 查看的时候 发现停止了</span></span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 后台运行，docker发现前台没有，容器启动后，发现自己没有提供服务，会立刻停止</span></span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Last login: Wed Jun 17 19:47:35 2020</span><br><span class="line">[root@192 ~]# systemctl start docker # 关机后重启了，需要启动docker</span><br><span class="line">[root@192 ~]# docker run -d centos # 运行</span><br><span class="line">8ce188e5fee31c2fac93c0a405ee1a95c38dbc50cb47c35b19c0039c27558ded</span><br><span class="line">[root@192 ~]# docker ps -a # 查看正在运行的</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES</span><br><span class="line">8ce188e5fee3        centos              &quot;/bin/bash&quot;         19 seconds ago      Exited (0) 18 seconds ago                       tender_dirac</span><br><span class="line">7b1a7dd10ea4        centos              &quot;/bin/bash&quot;         8 hours ago         Exited (0) 8 hours ago                          fervent_mirzakhani</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h4 id="查看日志"><a href="#查看日志" class="headerlink" title="查看日志"></a>查看日志</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker logs</span><br><span class="line">docker logs -f -t --tail n 【id】</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200617161744298.png" alt="image-20200617161744298"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@192 ~]# docker logs --helpUsage:	docker logs [OPTIONS] CONTAINERFetch the logs of a containerOptions:      --details        Show extra details provided to logs  -f, --follow         Follow log output      --since string   Show logs since timestamp (e.g.                       2013-01-02T13:23:37) or relative                       (e.g. 42m for 42 minutes)      --tail string    Number of lines to show from the                       end of the logs (default &quot;all&quot;)  -t, --timestamps     Show timestamps # 时间戳      --until string   Show logs before a timestamp (e.g.                       2013-01-02T13:23:37) or relative                       (e.g. 42m for 42 minutes)</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">whiel true;do echo shenzai;sleep </span><br></pre></td></tr></table></figure>



<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 运行一个[root@192 ~]<span class="comment"># docker run -it centos /bin/bash[root@c2887d35c71d /]# [root@192 ~]# docker psCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMESc2887d35c71d        centos              &quot;/bin/bash&quot;         57 seconds ago      Up 56 seconds                           vigorous_kare# 查看日志，由于没有运行脚本，所以啥也没显示[root@192 ~]# docker logs -f -t --tail 10 c2887d35c71d^C # ctrl+c退出# 运行centos里面加个脚本[root@192 ~]# docker run -d centos /bin/sh -c &quot;while true;do echo shenzai;sleep 1;done&quot;cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388[root@192 ~]# docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMEScb6d7fbc3f27        centos              &quot;/bin/sh -c &#x27;while t…&quot;   7 seconds ago       Up 6 seconds                            dreamy_almeidac2887d35c71d        centos              &quot;/bin/bash&quot;              3 minutes ago       Up 3 minutes                            vigorous_kare# 查看日志 发现隔一秒打印一条[root@192 ~]# docker logs -f -t --tail 10 cb6d7fbc3f272020-06-17T12:02:11.293765084Z shenzai2020-06-17T12:02:12.297675608Z shenzai2020-06-17T12:02:13.301845582Z shenzai2020-06-17T12:02:14.304800996Z shenzai2020-06-17T12:02:15.307130238Z shenzai2020-06-17T12:02:16.310574235Z shenzai2020-06-17T12:02:17.312946923Z shenzai2020-06-17T12:02:18.314841295Z shenzai2020-06-17T12:02:19.317021705Z shenzai2020-06-17T12:02:20.319670013Z shenzai2020-06-17T12:02:21.322651649Z shenzai2020-06-17T12:02:22.325466918Z shenzai2020-06-17T12:02:23.327984704Z shenzai2020-06-17T12:02:24.329656919Z shenzai</span></span></span><br></pre></td></tr></table></figure>

<h4 id="查看正在运行的容器信息"><a href="#查看正在运行的容器信息" class="headerlink" title="查看正在运行的容器信息"></a>查看正在运行的容器信息</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@192 ~]# docker inspect cb6d7fbc3f27[    &#123;        # 容器的完整id        &quot;Id&quot;: &quot;cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388&quot;,                # 创建时间        &quot;Created&quot;: &quot;2020-06-17T12:00:50.706906186Z&quot;,                # 脚本位置        &quot;Path&quot;: &quot;/bin/sh&quot;,                # 运行的脚本        &quot;Args&quot;: [            &quot;-c&quot;,            &quot;while true;do echo shenzai;sleep 1;done&quot;        ],        &quot;State&quot;: &#123;            &quot;Status&quot;: &quot;running&quot;, # 状态，正在运行            &quot;Running&quot;: true,            &quot;Paused&quot;: false,            &quot;Restarting&quot;: false,            &quot;OOMKilled&quot;: false,            &quot;Dead&quot;: false,            &quot;Pid&quot;: 1909, # 父进程id            &quot;ExitCode&quot;: 0,            &quot;Error&quot;: &quot;&quot;,            &quot;StartedAt&quot;: &quot;2020-06-17T12:00:51.093617477Z&quot;,            &quot;FinishedAt&quot;: &quot;0001-01-01T00:00:00Z&quot;        &#125;,                # 来源镜像        &quot;Image&quot;: &quot;sha256:831691599b88ad6cc2a4abbd0e89661a121aff14cfa289ad840fd3946f274f1f&quot;,        &quot;ResolvConfPath&quot;: &quot;/var/lib/docker/containers/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388/resolv.conf&quot;,        &quot;HostnamePath&quot;: &quot;/var/lib/docker/containers/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388/hostname&quot;,        &quot;HostsPath&quot;: &quot;/var/lib/docker/containers/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388/hosts&quot;,        &quot;LogPath&quot;: &quot;/var/lib/docker/containers/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388-json.log&quot;,                        &quot;Name&quot;: &quot;/dreamy_almeida&quot;,        &quot;RestartCount&quot;: 0,        &quot;Driver&quot;: &quot;overlay2&quot;,        &quot;Platform&quot;: &quot;linux&quot;,        &quot;MountLabel&quot;: &quot;&quot;,        &quot;ProcessLabel&quot;: &quot;&quot;,        &quot;AppArmorProfile&quot;: &quot;&quot;,        &quot;ExecIDs&quot;: null,                # 主机配置        &quot;HostConfig&quot;: &#123;            &quot;Binds&quot;: null,            &quot;ContainerIDFile&quot;: &quot;&quot;,            &quot;LogConfig&quot;: &#123;                &quot;Type&quot;: &quot;json-file&quot;,                &quot;Config&quot;: &#123;&#125;            &#125;,            &quot;NetworkMode&quot;: &quot;default&quot;,            &quot;PortBindings&quot;: &#123;&#125;,            &quot;RestartPolicy&quot;: &#123;                &quot;Name&quot;: &quot;no&quot;,                &quot;MaximumRetryCount&quot;: 0            &#125;,            &quot;AutoRemove&quot;: false,            &quot;VolumeDriver&quot;: &quot;&quot;,            &quot;VolumesFrom&quot;: null,            &quot;CapAdd&quot;: null,            &quot;CapDrop&quot;: null,            &quot;Capabilities&quot;: null,            &quot;Dns&quot;: [],            &quot;DnsOptions&quot;: [],            &quot;DnsSearch&quot;: [],            &quot;ExtraHosts&quot;: null,            &quot;GroupAdd&quot;: null,            &quot;IpcMode&quot;: &quot;private&quot;,            &quot;Cgroup&quot;: &quot;&quot;,            &quot;Links&quot;: null,            &quot;OomScoreAdj&quot;: 0,            &quot;PidMode&quot;: &quot;&quot;,            &quot;Privileged&quot;: false,            &quot;PublishAllPorts&quot;: false,            &quot;ReadonlyRootfs&quot;: false,            &quot;SecurityOpt&quot;: null,            &quot;UTSMode&quot;: &quot;&quot;,            &quot;UsernsMode&quot;: &quot;&quot;,            &quot;ShmSize&quot;: 67108864,            &quot;Runtime&quot;: &quot;runc&quot;,            &quot;ConsoleSize&quot;: [                0,                0            ],            &quot;Isolation&quot;: &quot;&quot;,            &quot;CpuShares&quot;: 0,            &quot;Memory&quot;: 0,            &quot;NanoCpus&quot;: 0,            &quot;CgroupParent&quot;: &quot;&quot;,            &quot;BlkioWeight&quot;: 0,            &quot;BlkioWeightDevice&quot;: [],            &quot;BlkioDeviceReadBps&quot;: null,            &quot;BlkioDeviceWriteBps&quot;: null,            &quot;BlkioDeviceReadIOps&quot;: null,            &quot;BlkioDeviceWriteIOps&quot;: null,            &quot;CpuPeriod&quot;: 0,            &quot;CpuQuota&quot;: 0,            &quot;CpuRealtimePeriod&quot;: 0,            &quot;CpuRealtimeRuntime&quot;: 0,            &quot;CpusetCpus&quot;: &quot;&quot;,            &quot;CpusetMems&quot;: &quot;&quot;,            &quot;Devices&quot;: [],            &quot;DeviceCgroupRules&quot;: null,            &quot;DeviceRequests&quot;: null,            &quot;KernelMemory&quot;: 0,            &quot;KernelMemoryTCP&quot;: 0,            &quot;MemoryReservation&quot;: 0,            &quot;MemorySwap&quot;: 0,            &quot;MemorySwappiness&quot;: null,            &quot;OomKillDisable&quot;: false,            &quot;PidsLimit&quot;: null,            &quot;Ulimits&quot;: null,            &quot;CpuCount&quot;: 0,            &quot;CpuPercent&quot;: 0,            &quot;IOMaximumIOps&quot;: 0,            &quot;IOMaximumBandwidth&quot;: 0,            &quot;MaskedPaths&quot;: [                &quot;/proc/asound&quot;,                &quot;/proc/acpi&quot;,                &quot;/proc/kcore&quot;,                &quot;/proc/keys&quot;,                &quot;/proc/latency_stats&quot;,                &quot;/proc/timer_list&quot;,                &quot;/proc/timer_stats&quot;,                &quot;/proc/sched_debug&quot;,                &quot;/proc/scsi&quot;,                &quot;/sys/firmware&quot;            ],            &quot;ReadonlyPaths&quot;: [                &quot;/proc/bus&quot;,                &quot;/proc/fs&quot;,                &quot;/proc/irq&quot;,                &quot;/proc/sys&quot;,                &quot;/proc/sysrq-trigger&quot;            ]        &#125;,                # 其他配置        &quot;GraphDriver&quot;: &#123;            &quot;Data&quot;: &#123;                &quot;LowerDir&quot;: &quot;/var/lib/docker/overlay2/3675586ebbd79cd72d2562a90c9380627a331c563724c0dac091f92600af4907-init/diff:/var/lib/docker/overlay2/7f79322e0f58d651a84a555dadd83d92537788172525945d3f538dd95dce336c/diff&quot;,                &quot;MergedDir&quot;: &quot;/var/lib/docker/overlay2/3675586ebbd79cd72d2562a90c9380627a331c563724c0dac091f92600af4907/merged&quot;,                &quot;UpperDir&quot;: &quot;/var/lib/docker/overlay2/3675586ebbd79cd72d2562a90c9380627a331c563724c0dac091f92600af4907/diff&quot;,                &quot;WorkDir&quot;: &quot;/var/lib/docker/overlay2/3675586ebbd79cd72d2562a90c9380627a331c563724c0dac091f92600af4907/work&quot;            &#125;,            &quot;Name&quot;: &quot;overlay2&quot;        &#125;,                &quot;Mounts&quot;: [], # 挂载                # 基本配置        &quot;Config&quot;: &#123;            &quot;Hostname&quot;: &quot;cb6d7fbc3f27&quot;,            &quot;Domainname&quot;: &quot;&quot;,            &quot;User&quot;: &quot;&quot;,            &quot;AttachStdin&quot;: false,            &quot;AttachStdout&quot;: false,            &quot;AttachStderr&quot;: false,            &quot;Tty&quot;: false,            &quot;OpenStdin&quot;: false,            &quot;StdinOnce&quot;: false,            &quot;Env&quot;: [                &quot;PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin&quot;            ], # 基本环境变量，这里没有Java                        # 基本命令            &quot;Cmd&quot;: [                &quot;/bin/sh&quot;,                &quot;-c&quot;,                &quot;while true;do echo shenzai;sleep 1;done&quot;            ],            &quot;Image&quot;: &quot;centos&quot;,            &quot;Volumes&quot;: null,            &quot;WorkingDir&quot;: &quot;&quot;,            &quot;Entrypoint&quot;: null,            &quot;OnBuild&quot;: null,            &quot;Labels&quot;: &#123;                &quot;org.label-schema.build-date&quot;: &quot;20200611&quot;,                &quot;org.label-schema.license&quot;: &quot;GPLv2&quot;,                &quot;org.label-schema.name&quot;: &quot;CentOS Base Image&quot;,                &quot;org.label-schema.schema-version&quot;: &quot;1.0&quot;,                &quot;org.label-schema.vendor&quot;: &quot;CentOS&quot;            &#125;        &#125;,                # 网卡，比如现在用的是桥接的网卡        &quot;NetworkSettings&quot;: &#123;            &quot;Bridge&quot;: &quot;&quot;,            &quot;SandboxID&quot;: &quot;4d701985d7e77aa153790b697b2f38a61e20555c224b7675e4bf650b82799882&quot;,            &quot;HairpinMode&quot;: false,            &quot;LinkLocalIPv6Address&quot;: &quot;&quot;,            &quot;LinkLocalIPv6PrefixLen&quot;: 0,            &quot;Ports&quot;: &#123;&#125;,            &quot;SandboxKey&quot;: &quot;/var/run/docker/netns/4d701985d7e7&quot;,            &quot;SecondaryIPAddresses&quot;: null,            &quot;SecondaryIPv6Addresses&quot;: null,            &quot;EndpointID&quot;: &quot;8a6c71e2bafb19ca7dfd85445ccc4bef6d17467360a243d624089e676a24a018&quot;,            &quot;Gateway&quot;: &quot;172.17.0.1&quot;,            &quot;GlobalIPv6Address&quot;: &quot;&quot;,            &quot;GlobalIPv6PrefixLen&quot;: 0,            &quot;IPAddress&quot;: &quot;172.17.0.3&quot;,            &quot;IPPrefixLen&quot;: 16,            &quot;IPv6Gateway&quot;: &quot;&quot;,            &quot;MacAddress&quot;: &quot;02:42:ac:11:00:03&quot;,            &quot;Networks&quot;: &#123;                &quot;bridge&quot;: &#123;                    &quot;IPAMConfig&quot;: null,                    &quot;Links&quot;: null,                    &quot;Aliases&quot;: null,                    &quot;NetworkID&quot;: &quot;22b0fd2290ccbc4e066a75d3f01bd8bf32ee4352c5bbcfc9f911287219219571&quot;,                    &quot;EndpointID&quot;: &quot;8a6c71e2bafb19ca7dfd85445ccc4bef6d17467360a243d624089e676a24a018&quot;,                    &quot;Gateway&quot;: &quot;172.17.0.1&quot;,                    &quot;IPAddress&quot;: &quot;172.17.0.3&quot;,                    &quot;IPPrefixLen&quot;: 16,                    &quot;IPv6Gateway&quot;: &quot;&quot;,                    &quot;GlobalIPv6Address&quot;: &quot;&quot;,                    &quot;GlobalIPv6PrefixLen&quot;: 0,                    &quot;MacAddress&quot;: &quot;02:42:ac:11:00:03&quot;,                    &quot;DriverOpts&quot;: null                &#125;            &#125;        &#125;    &#125;]</span><br></pre></td></tr></table></figure>



<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 停止正在疯狂输出的那个容器[root@192 ~]<span class="comment"># docker stop cb6d7fbc3f27cb6d7fbc3f27</span></span></span><br></pre></td></tr></table></figure>

<h4 id="进入当前正在运行的容器"><a href="#进入当前正在运行的容器" class="headerlink" title="进入当前正在运行的容器"></a>进入当前正在运行的容器</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 我们通常容器都是使用后台方式运行的edocker <span class="built_in">exec</span> -it 容器id bashSHELL<span class="comment"># 测试[root@192 ~]# docker psCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMESc2887d35c71d        centos              &quot;/bin/bash&quot;         35 minutes ago      Up 35 minutes                           vigorous_kare[root@192 ~]# docker exec -it c2887d35c71d /bin/bash[root@c2887d35c71d /]# lsbin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  vardev  home  lib64  media       opt  root  sbin  sys  usr[root@c2887d35c71d /]# ps -efUID         PID   PPID  C STIME TTY          TIME CMDroot          1      0  0 11:57 pts/0    00:00:00 /bin/bashroot         14      0  0 12:32 pts/1    00:00:00 /bin/bashroot         28     14  0 12:32 pts/1    00:00:00 ps -ef[root@c2887d35c71d /]# c2887d35c71d[root@c2887d35c71d /]# exitexit# 方式二[root@192 ~]# docker attach c2887d35c71d[root@c2887d35c71d /]# # 区别# docker exec # 进入容器后开启一个新的终端，可以在里面操作(常用)# docker attach 进入容器正在执行的终端，不会启动新的进程</span></span></span><br></pre></td></tr></table></figure>



<h4 id="从容器内拷贝文件到主机上"><a href="#从容器内拷贝文件到主机上" class="headerlink" title="从容器内拷贝文件到主机上"></a>从容器内拷贝文件到主机上</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 运行</span></span><br><span class="line">[root@192 ~]# docker run -it centos</span><br><span class="line"><span class="meta">#</span><span class="bash"> ctrl P Q 不关闭退出，查看</span></span><br><span class="line">[root@0569081aa89c /]# [root@192 ~]# docker ps</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES</span><br><span class="line">0569081aa89c        centos              &quot;/bin/bash&quot;         19 seconds ago      Up 19 seconds    </span><br><span class="line">hopeful_chebyshev</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看主机home下无文件</span></span><br><span class="line">[root@192 ~]# cd /home</span><br><span class="line">[root@192 home]# ls</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 进入正在运行的容器</span></span><br><span class="line">[root@192 home]# docker attach 0569081aa89c</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 进入容器home目录</span></span><br><span class="line">[root@0569081aa89c /]# cd /home</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 在目录中创建java文件</span></span><br><span class="line">[root@0569081aa89c home]# touch test.java</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 退出并停止容器</span></span><br><span class="line">[root@0569081aa89c home]# exit</span><br><span class="line">exit</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看现在运行的容器</span></span><br><span class="line">[root@192 home]# docker ps</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 容器虽然被停止，但是数据都会保留</span></span><br><span class="line">[root@192 home]# docker ps -a</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES</span><br><span class="line">0569081aa89c        centos              &quot;/bin/bash&quot;              3 minutes ago       Exited (0) 8 seconds ago                           hopeful_chebyshev</span><br><span class="line">f589e5684a01        centos              &quot;/bin/bash&quot;              44 minutes ago      Exited (0) 44 minutes ago                          cranky_easley</span><br><span class="line">cb6d7fbc3f27        centos              &quot;/bin/sh -c &#x27;while t…&quot;   54 minutes ago      Exited (137) 42 minutes ago                        dreamy_almeida</span><br><span class="line">c2887d35c71d        centos              &quot;/bin/bash&quot;              58 minutes ago      Exited (127) 16 minutes ago                        vigorous_kare</span><br><span class="line">8ce188e5fee3        centos              &quot;/bin/bash&quot;              About an hour ago   Exited (0) About an hour ago                       tender_dirac</span><br><span class="line">7b1a7dd10ea4        centos              &quot;/bin/bash&quot;              9 hours ago         Exited (0) 9 hours ago                             fervent_mirzakhani</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 容器数据拷贝到主机</span></span><br><span class="line">[root@192 home]# docker cp 0569081aa89c:/home/test.java /home</span><br><span class="line">[root@192 home]# ls</span><br><span class="line">test.java</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 拷贝是一个手动过程，未来我们使用 -v 卷的技术，可以实现自动同步 /home /home</span></span><br></pre></td></tr></table></figure>

<h4 id="查看内容占用"><a href="#查看内容占用" class="headerlink" title="查看内容占用"></a>查看内容占用</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker stats</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h4 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h4><p><img src="Docker.assets/image-20200617210554147.png" alt="image-20200617210554147"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@192 home]# docker --helpUsage:	docker [OPTIONS] COMMANDA self-sufficient runtime for containersOptions:      --config string      Location of client config                           files (default &quot;/root/.docker&quot;)  -c, --context string     Name of the context to use to                           connect to the daemon                           (overrides DOCKER_HOST env var                           and default context set with                           &quot;docker context use&quot;)  -D, --debug              Enable debug mode  -H, --host list          Daemon socket(s) to connect to  -l, --log-level string   Set the logging level                           (&quot;debug&quot;|&quot;info&quot;|&quot;warn&quot;|&quot;error&quot;|&quot;fatal&quot;) (default &quot;info&quot;)      --tls                Use TLS; implied by --tlsverify      --tlscacert string   Trust certs signed only by                           this CA (default                           &quot;/root/.docker/ca.pem&quot;)      --tlscert string     Path to TLS certificate file                           (default &quot;/root/.docker/cert.pem&quot;)      --tlskey string      Path to TLS key file (default                           &quot;/root/.docker/key.pem&quot;)      --tlsverify          Use TLS and verify the remote  -v, --version            Print version information and quitManagement Commands:  builder     Manage builds  config      Manage Docker configs  container   Manage containers  context     Manage contexts  engine      Manage the docker engine  image       Manage images  network     Manage networks  node        Manage Swarm nodes  plugin      Manage plugins  secret      Manage Docker secrets  service     Manage services  stack       Manage Docker stacks  swarm       Manage Swarm  system      Manage Docker  trust       Manage trust on Docker images  volume      Manage volumesCommands:  attach      Attach local standard input, output, and error streams to a running container  build       Build an image from a Dockerfile  commit      Create a new image from a container&#x27;s changes  cp          Copy files/folders between a container and the local filesystem  create      Create a new container  diff        Inspect changes to files or directories on a container&#x27;s filesystem  events      Get real time events from the server  exec        Run a command in a running container  export      Export a container&#x27;s filesystem as a tar archive  history     Show the history of an image  images      List images  import      Import the contents from a tarball to create a filesystem image  info        Display system-wide information  inspect     Return low-level information on Docker objects  kill        Kill one or more running containers  load        Load an image from a tar archive or STDIN  login       Log in to a Docker registry  logout      Log out from a Docker registry  logs        Fetch the logs of a container  pause       Pause all processes within one or more containers  port        List port mappings or a specific mapping for the container  ps          List containers  pull        Pull an image or a repository from a registry  push        Push an image or a repository to a registry  rename      Rename a container  restart     Restart one or more containers  rm          Remove one or more containers  rmi         Remove one or more images  run         Run a command in a new container  save        Save one or more images to a tar archive (streamed to STDOUT by default)  search      Search the Docker Hub for images  start       Start one or more stopped containers  stats       Display a live stream of container(s) resource usage statistics  stop        Stop one or more running containers  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE  top         Display the running processes of a container  unpause     Unpause all processes within one or more containers  update      Update configuration of one or more containers  version     Show the Docker version information  wait        Block until one or more containers stop, then print their exit codes</span><br></pre></td></tr></table></figure>



<p><img src="Docker.assets/image-20200617210932306.png" alt="image-20200617210932306"></p>
<p><img src="Docker.assets/image-20200617211021003.png" alt="image-20200617211021003"></p>
<p><img src="Docker.assets/image-20200617211039508.png" alt="image-20200617211039508"></p>
<h4 id="作业练习"><a href="#作业练习" class="headerlink" title="作业练习"></a>作业练习</h4><h5 id="部署Nginx"><a href="#部署Nginx" class="headerlink" title="部署Nginx"></a>部署Nginx</h5><p><img src="Docker.assets/image-20200618100621199.png" alt="image-20200618100621199"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 官网搜索nginx，可以看到帮助文档</span></span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 下载镜像</span></span><br><span class="line">[root@192 home]# docker pull nginx</span><br><span class="line">Using default tag: latest</span><br><span class="line">latest: Pulling from library/nginx</span><br><span class="line">8559a31e96f4: Pull complete </span><br><span class="line">8d69e59170f7: Pull complete </span><br><span class="line">3f9f1ec1d262: Pull complete </span><br><span class="line">d1f5ff4f210d: Pull complete </span><br><span class="line">1e22bfa8652e: Pull complete </span><br><span class="line">Digest: sha256:21f32f6c08406306d822a0e6e8b7dc81f53f336570e852e25fbe1e3e3d0d0133</span><br><span class="line">Status: Downloaded newer image for nginx:latest</span><br><span class="line">docker.io/library/nginx:latest</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看镜像</span></span><br><span class="line">[root@192 home]# docker images</span><br><span class="line">REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE</span><br><span class="line">centos              latest              831691599b88        13 hours ago        215MB</span><br><span class="line">nginx               latest              2622e6cca7eb        7 days ago          132MB</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 运行测试</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> -d 后台运行，--name 命名，-p 暴露端口，3344服务器、宿主机的端口，容器内部端口</span></span><br><span class="line">[root@192 home]# docker run -d --name nginx01 -p:3344:80 nginx</span><br><span class="line">38dbf7bdcaef232d269b7184d91e44e06087181b5ee929494e177ad526810fa8</span><br><span class="line">[root@192 home]# docker ps</span><br><span class="line">CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES</span><br><span class="line">38dbf7bdcaef        nginx               &quot;/docker-entrypoint.…&quot;   7 seconds ago       Up 6 seconds        0.0.0.0:3344-&gt;80/tcp   nginx01</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 使用3344可以访问成功</span></span><br><span class="line">[root@192 home]# curl localhost:3344</span><br><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">&lt;title&gt;Welcome to nginx!&lt;/title&gt;</span><br><span class="line">&lt;style&gt;</span><br><span class="line">    body &#123;</span><br><span class="line">        width: 35em;</span><br><span class="line">        margin: 0 auto;</span><br><span class="line">        font-family: Tahoma, Verdana, Arial, sans-serif;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">&lt;h1&gt;Welcome to nginx!&lt;/h1&gt;</span><br><span class="line">&lt;p&gt;If you see this page, the nginx web server is successfully installed and</span><br><span class="line">working. Further configuration is required.&lt;/p&gt;</span><br><span class="line"></span><br><span class="line">&lt;p&gt;For online documentation and support please refer to</span><br><span class="line">&lt;a href=&quot;http://nginx.org/&quot;&gt;nginx.org&lt;/a&gt;.&lt;br/&gt;</span><br><span class="line">Commercial support is available at</span><br><span class="line">&lt;a href=&quot;http://nginx.com/&quot;&gt;nginx.com&lt;/a&gt;.&lt;/p&gt;</span><br><span class="line"></span><br><span class="line">&lt;p&gt;&lt;em&gt;Thank you for using nginx.&lt;/em&gt;&lt;/p&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>端口暴露</p>
<p><img src="Docker.assets/image-20200617212310709.png" alt="image-20200617212310709"></p>
<p>可以公网访问</p>
<p>找到服务器地址</p>
<p><img src="Docker.assets/image-20200617213213721.png" alt="image-20200617213213721"></p>
<p>浏览器输入 192.168.147.132:3344/</p>
<p><img src="Docker.assets/image-20200617213142079.png" alt="image-20200617213142079"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@192 home]# docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES38dbf7bdcaef        nginx               &quot;/docker-entrypoint.…&quot;   21 minutes ago      Up 21 minutes       0.0.0.0:3344-&gt;80/tcp   nginx01# 进入容器[root@192 home]# docker exec -it nginx01 /bin/bash# 查一下nginx在哪root@38dbf7bdcaef:/# whereis nginxnginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx# 到这个目录root@38dbf7bdcaef:/# cd /etc/nginxroot@38dbf7bdcaef:/etc/nginx# lsconf.d		koi-utf  mime.types  nginx.conf   uwsgi_paramsfastcgi_params	koi-win  modules     scgi_params  win-utf# 退出root@38dbf7bdcaef:/etc/nginx# exitexit# 停止[root@192 home]# docker stop 38dbf7bdcaef38dbf7bdcaef</span><br></pre></td></tr></table></figure>



<p>再次刷新网页，服务关闭</p>
<p>思考问题：每次改动nginx配置文件，都需要进入容器内部，十分麻烦，要是可以在容器外部提供一个映射路径，达到在容器修改文件名，容器内部就可以自动修改？-v 数据卷技术！</p>
<h5 id="部署tomcat"><a href="#部署tomcat" class="headerlink" title="部署tomcat"></a>部署tomcat</h5><p><img src="Docker.assets/image-20200618100551587.png" alt="image-20200618100551587"></p>
<p>【视频书签：<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1og4y1q7M4?p=15%E3%80%91">https://www.bilibili.com/video/BV1og4y1q7M4?p=15】</a></p>
<p>【20200618毕竟生产力工具，加油搞，这周一定刷完！！每次关机都要重连，麻烦(<em>╹▽╹</em>)】</p>
<p>在docker hub上查看版本号和使用方法</p>
<p><img src="Docker.assets/image-20200618100319796.png" alt="image-20200618100319796"></p>
<p>官方文档一定要翻烂，超多版本，我的天呐~</p>
<h6 id="官方方法"><a href="#官方方法" class="headerlink" title="官方方法"></a>官方方法</h6><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it --rm tomcat:9.0# docker run 可以不用pull，能自动下载# -it 直接进去运行# --rm 是什么意思？入门的意思？# 我们之前的启动都是后台，停止了容器之后，容器还是可以查到# 写了--rm，类似阅后即焚模式，用完即删除，这种通常用来测试# 最后冒号查好的版本号</span><br></pre></td></tr></table></figure>



<p><img src="Docker.assets/image-20200618101811914.png" alt="image-20200618101811914"></p>
<p><img src="Docker.assets/image-20200618101849917.png" alt="image-20200618101849917"></p>
<p>ctrl+c退出</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker ps -a</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618102022167.png" alt="image-20200618102022167"></p>
<p>可以看到并没有tomcat，印证阅后即焚模式，容器会删除，镜像不会删除</p>
<p>平时不建议这样搞</p>
<h6 id="正常方法"><a href="#正常方法" class="headerlink" title="正常方法"></a>正常方法</h6><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull tomcat:9.0 # 之前下过了，应该不用下了，这里老师讲错了</span><br></pre></td></tr></table></figure>



<p><img src="Docker.assets/image-20200618102544564.png" alt="image-20200618102544564"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 启动运行，应该加上版本号docker run -d -p 3355:8080 --name tomcat01 tomcat</span></span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618102837397.png" alt="image-20200618102837397"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 进入容器</span></span><br><span class="line">docker exec -it tomcat01 /bin/bash</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618103109004.png" alt="image-20200618103109004"></p>
<p>发现问题</p>
<ol>
<li>linux命令少了</li>
<li>没有webapps</li>
</ol>
<p><img src="Docker.assets/image-20200618103407205.png" alt="image-20200618103407205"></p>
<p>这是阿里云镜像的原因：默认使用最小镜像，所有不必要的都剔除了，保证最小可运行环境</p>
<p><img src="Docker.assets/image-20200618103848104.png" alt="image-20200618103848104"></p>
<p>再次找到结构</p>
<p><img src="Docker.assets/image-20200618104052242.png" alt="image-20200618104052242"></p>
<p>在浏览器中输入：<a target="_blank" rel="noopener" href="http://192.168.147.132:3355/">http://192.168.147.132:3355/</a></p>
<p><img src="Docker.assets/image-20200618103958168.png" alt="image-20200618103958168"></p>
<p>思考问题：我们以后部署项目，如果每次都要进入容器是不是身份麻烦？我要是可以在容器外部提供一个映射路径，webapps，我们在外部放置项目，就自动同步到内部就好了！</p>
<p>docker容器 tomcat+网站 </p>
<p>docker mysql</p>
<h5 id="部署es-kibana"><a href="#部署es-kibana" class="headerlink" title="部署es+kibana"></a>部署es+kibana</h5><p><img src="Docker.assets/image-20200618104950722.png" alt="image-20200618104950722"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> es 暴露的端口很多<span class="comment"># es 十分耗内存# es 的数据一般需要放置到安全目录！挂载# 启动 elasticsearch$ docker run -d --name elasticsearch01 -p 9200:9200 -p 9300:9300 -e &quot;discovery.type=single-node&quot; elasticsearch:7.6.2# 查看内存占用情况docker stats</span></span></span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618111713885.png" alt="image-20200618111713885"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 先感觉stop一下docker stop ba18713ca536</span></span><br></pre></td></tr></table></figure>



<p><img src="Docker.assets/image-20200618105057785.png" alt="image-20200618105057785"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 通过 -e 限制内存$ docker run -d --name elasticsearch02 -p 9200:9200 -p 9300:9300 -e <span class="string">&quot;discovery.type=single-node&quot;</span> -e ES_JAVA_OPTS=“-Xms64m -Xmx512m” elasticsearch:7.6.2</span></span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618113018622.png" alt="image-20200618113018622"></p>
<p>没成功啊，SEI能告诉我为啥！！</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -d --name elasticsearch02 -p 9200:9200 -p 9300:9300 -e &quot;discovery.type=single-node&quot; -e ES_JAVA_OPTS=&quot;-Xms64m -Xmx512m&quot; elasticsearch:7.6.2</span><br></pre></td></tr></table></figure>

<blockquote>
<p>py0003null大佬:<br>“ES_JAVA_OPTS=-Xms64m -Xmx512m” </p>
<p>py0003null大佬:<br>引号提前试试</p>
</blockquote>
<p>原因是引号！！你没觉得怪怪的嘛</p>
<p><img src="Docker.assets/image-20200618115302656.png" alt="image-20200618115302656"></p>
<p>此时查看stats，发现内存占用在控制范围内</p>
<p><img src="Docker.assets/image-20200618115149971.png" alt="image-20200618115149971"></p>
<p>ctrl + C退出，记得stop</p>
<p><img src="Docker.assets/image-20200618115921069.png" alt="image-20200618115921069"></p>
<h5 id="思考：用kibana链接elasticsearch"><a href="#思考：用kibana链接elasticsearch" class="headerlink" title="思考：用kibana链接elasticsearch"></a>思考：用kibana链接elasticsearch</h5><p><img src="Docker.assets/image-20200618113556445.png" alt="image-20200618113556445"></p>
<h3 id="可视化"><a href="#可视化" class="headerlink" title="可视化"></a>可视化</h3><ul>
<li>portainer(先用这个)</li>
<li>Rancher(CI/CD时用)</li>
</ul>
<h4 id="portainer"><a href="#portainer" class="headerlink" title="portainer"></a>portainer</h4><p>Docker图像化界面管理工具，提供一个后台面板供我们操作！</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker --privileged=true portainer/portainer</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618114542622.png" alt="image-20200618114542622"></p>
<p>访问外网8088</p>
<p><img src="Docker.assets/image-20200618114902169.png" alt="image-20200618114902169"></p>
<p>用户名</p>
<p>密码 </p>
<p><img src="Docker.assets/image-20200618134706491.png" alt="image-20200618134706491"></p>
<p>【中午吃饭重连了一下，失败，不知道发生了啥】</p>
<p><img src="Docker.assets/image-20200618135424706.png" alt="image-20200618135424706"></p>
<p><img src="Docker.assets/image-20200618135450037.png" alt="image-20200618135450037"></p>
<p>【平时不会用这个，好吧，先往下】</p>
<h3 id=""><a href="#" class="headerlink" title=""></a></h3><h2 id="Docker镜像"><a href="#Docker镜像" class="headerlink" title="Docker镜像"></a>Docker镜像</h2><h3 id="原理"><a href="#原理" class="headerlink" title="原理"></a>原理</h3><p>UnionFS 联合文件系统</p>
<p><img src="Docker.assets/image-20200618140242423.png" alt="image-20200618140242423"></p>
<p>bootfs：boot file system</p>
<p>rootfs：root file system</p>
<p><img src="Docker.assets/image-20200618140907894.png" alt="image-20200618140907894"></p>
<p><img src="Docker.assets/image-20200618140932621.png" alt="image-20200618140932621"></p>
<p><img src="Docker.assets/image-20200618141014511.png" alt="image-20200618141014511"></p>
<p><img src="Docker.assets/image-20200618153329894.png" alt="image-20200618153329894"></p>
<p>Docker镜像都是只读的，当容器启动时，一个新的可写层被加到镜像的顶部，这一层就是我们通常说的容器层，容器层之下的都叫镜像层</p>
<p><img src="Docker.assets/image-20200618153855605.png" alt="image-20200618153855605"></p>
<h3 id="commit提交镜像"><a href="#commit提交镜像" class="headerlink" title="commit提交镜像"></a>commit提交镜像</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker commit # 提交容器成为一个新的副本docker commit -m=&quot;提交的描述信息&quot; -a=&quot;作者&quot; 容器id 目标镜像名：[TAG]</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker imagesdocker run -it -p 8080:8080 tomcat</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618154457286.png" alt="image-20200618154457286"></p>
<p>这是一个前台程序</p>
<p><img src="Docker.assets/image-20200618154736475.png" alt="image-20200618154736475"></p>
<p>将webapps.dist里面所有的文件拷贝到webapps里面，其中-r必须有，表示目录递归拷贝</p>
<p><img src="Docker.assets/image-20200618154943194.png" alt="image-20200618154943194"></p>
<p><img src="Docker.assets/image-20200618155750591.png" alt="image-20200618155750591"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker commit -a=&quot;paidaxing&quot; -m=&quot;add webapps app&quot; 当前容器的id tomcat02:1.0</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618160425265.png" alt="image-20200618160425265"></p>
<p>发现新的版本，比之前的大了一些，因为里面记录了我们的改动</p>
<blockquote>
<p>如果想保存当前容器的状态，可以通过commit提交，获得一个镜像</p>
<p>好比我们以前学习VM的时候的快照</p>
<p>到这里算是入门了</p>
<p>接下来三个部分是docker的精髓</p>
</blockquote>
<h2 id="容器数据卷"><a href="#容器数据卷" class="headerlink" title="容器数据卷"></a>容器数据卷</h2><h3 id="什么是容器卷"><a href="#什么是容器卷" class="headerlink" title="什么是容器卷"></a>什么是容器卷</h3><p>docker是要将应用和环境打包成一个镜像</p>
<p>这样，数据就不应该在容器中，否则容器删除，数据就会丢失，这就是删库跑路</p>
<p>故容器之间要有一个数据共享技术</p>
<p>在Docker容器中产生的数据，同步到本地，这就是卷技术</p>
<p>本质上是一个目录挂载，将容器内的目录挂载到虚拟机上</p>
<p><img src="Docker.assets/image-20200618162917672.png" alt="image-20200618162917672"></p>
<p>目的：容器的持久化和同步操作</p>
<p>容器间可以数据共享</p>
<h3 id="使用数据卷"><a href="#使用数据卷" class="headerlink" title="使用数据卷"></a>使用数据卷</h3><p>方式一：直接使用命令来挂载</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker run -it -v -p</span><br><span class="line"><span class="meta">#</span><span class="bash"> -it 交互式进入</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> -v volume卷技术</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> -p 主机端口</span></span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618163659503.png" alt="image-20200618163659503"></p>
<p>新开一个窗口</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker inspect 容器id</span><br></pre></td></tr></table></figure>



<p><img src="Docker.assets/image-20200618163938466.png" alt="image-20200618163938466"></p>
<p>找到挂载信息Mounts</p>
<p><img src="Docker.assets/image-20200618164148642.png" alt="image-20200618164148642"></p>
<p>测试</p>
<p><img src="Docker.assets/image-20200618164818624.png" alt="image-20200618164818624"></p>
<p>容器停止后，修改主机文件，再启动容器的时候，数据同样改变</p>
<p>双向同步</p>
<h3 id="实战安装mysql"><a href="#实战安装mysql" class="headerlink" title="实战安装mysql"></a>实战安装mysql</h3><p>MySQL的数据持久化命令</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">docker search mysql</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 拉取</span></span><br><span class="line">docker pull mysql:5.7</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 挂载</span></span><br><span class="line">docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql5.7</span><br><span class="line"></span><br><span class="line">-d 后台运行</span><br><span class="line">-p 端口映射</span><br><span class="line">-v 卷挂载</span><br><span class="line">-e 环境配置 安装启动mysql需要配置密码</span><br><span class="line">--name 容器名字</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618171353336.png" alt="image-20200618171353336"></p>
<p>链接测试：打开SQLyog</p>
<p><img src="Docker.assets/image-20200618173234347.png" alt="image-20200618173234347"></p>
<p>点 测试链接</p>
<p><img src="Docker.assets/image-20200618173300551.png" alt="image-20200618173300551"></p>
<p>点 链接</p>
<p><img src="Docker.assets/image-20200618173729692.png" alt="image-20200618173729692"></p>
<h3 id="具名和匿名挂载"><a href="#具名和匿名挂载" class="headerlink" title="具名和匿名挂载"></a>具名和匿名挂载</h3><p>【视频书签：<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1og4y1q7M4?p=23%EF%BC%8C%E6%9C%89%E7%82%B9%E7%B4%AF%E4%BA%86%EF%BC%8C%E8%B7%9F%E4%B8%8D%E5%8A%A8%E4%BA%86%EF%BC%8C%E8%BF%99%E4%B8%A4%E5%A4%A9%E8%BF%90%E5%8A%A8%E5%A4%AA%E5%B0%91%E4%BA%86%EF%BC%8C%E6%9C%89%E7%82%B9%E9%A2%93%E3%80%91">https://www.bilibili.com/video/BV1og4y1q7M4?p=23，有点累了，跟不动了，这两天运动太少了，有点颓】</a></p>
<p><img src="Docker.assets/image-20200618203452205.png" alt="image-20200618203452205"></p>
<p><img src="Docker.assets/image-20200618203744983.png" alt="image-20200618203744983"></p>
<h3 id="-1"><a href="#-1" class="headerlink" title=""></a></h3><p>DockerFile使用来构建docker镜像的文件</p>
<p><img src="Docker.assets/image-20200618211547398.png" alt="image-20200618211547398"></p>
<blockquote>
<p>kas:<br>没有安装吧？</p>
<p>kas:<br>安装一下就可以了</p>
<p>py9001021曾叙坚:<br>centos 默认使用vi写内容，vim需要下载</p>
<p>py9001021曾叙坚:<br>是的，需要下载vim才能使用</p>
<p>YF:<br>配置可以改下，字体就很炫酷了</p>
<p>都建民:<br>用vi  试试</p>
<p>都建民:<br>你没有安装vim</p>
<p>都建民:<br>yum install vim</p>
<p>都建民:<br>试试这个命令</p>
</blockquote>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install vim # 编辑文件的，没有装一下</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618211516566.png" alt="image-20200618211516566"></p>
<p>安装完之后就可以运行这个命令了</p>
<p><img src="Docker.assets/image-20200618211803012.png" alt="image-20200618211803012"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 镜像是一层一层的，脚本是一行一行的<span class="comment"># 指令都是大写的# 这里的每个命令可以理解为镜像的一层FROM centosVOLUME [&quot;volume01&quot;,&quot;volume02&quot;] # 再创建镜像的时候就挂载出来CMD echo &quot;---end---&quot;CMD /bin/bash</span></span></span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618211023834.png" alt="image-20200618211023834"></p>
<p>想保存并退出</p>
<blockquote>
<p>Wesley.:<br>shift  加  冒号</p>
</blockquote>
<p><img src="Docker.assets/image-20200618211318410.png" alt="image-20200618211318410"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cat dockerfile1</span><br><span class="line"></span><br><span class="line">docker build -f dockerfile1 -t padaxing/centos:1.0 .  # 最后的点很重要 镜像名不能有/</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618212936481.png" alt="image-20200618212936481"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker images</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200618213310752.png" alt="image-20200618213310752"></p>
<p>启动生成的镜像</p>
<p><img src="Docker.assets/image-20200618220658979.png" alt="image-20200618220658979"></p>
<p>在容器内部创建一个文件</p>
<p><img src="Docker.assets/image-20200618221801103.png" alt="image-20200618221801103"></p>
<p>查看Mounts，Source对应容器外目录，匿名挂载卷</p>
<p><img src="Docker.assets/image-20200618221837895.png" alt="image-20200618221837895"></p>
<p>测试一下，在container volume01下生成文件</p>
<p><img src="Docker.assets/image-20200618222224352.png" alt="image-20200618222224352"></p>
<p>在主机挂载路径下，也同样生成</p>
<h3 id="多个容器数据共享"><a href="#多个容器数据共享" class="headerlink" title="多个容器数据共享"></a>多个容器数据共享</h3><p>【视频书签：<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1og4y1q7M4?p=25%EF%BC%8C%E5%91%A8%E6%97%A5%E4%B8%8B%E5%8D%88%EF%BC%8C%E6%BB%A8%E6%B1%9F%EF%BC%8C%E5%A4%A7%E9%9B%A8%EF%BC%8C%E6%9C%AC%E6%9D%A5%E8%AF%B4%E8%BF%99%E5%91%A8%E5%85%AD%E5%88%B7%E5%AE%8C%E7%9A%84%EF%BC%8C%E5%8F%91%E7%8E%B0%E5%91%A8%E5%85%AD%E5%95%A5%E4%B9%9F%E5%B9%B2%E4%B8%8D%E4%B8%8B%E5%8E%BB%EF%BC%8C%E9%99%A4%E4%BA%86%E5%90%83%E5%B0%B1%E6%98%AF%E7%9D%A1%EF%BC%8C%E8%A6%81%E4%B9%88%E5%8F%91%E5%91%86%E7%A0%94%E7%A9%B6%E5%B0%8F%E9%87%91%E5%BA%93%E3%80%91">https://www.bilibili.com/video/BV1og4y1q7M4?p=25，周日下午，滨江，大雨，本来说这周六刷完的，发现周六啥也干不下去，除了吃就是睡，要么发呆研究小金库】</a></p>
<p><img src="Docker.assets/image-20200621165403842.png" alt="image-20200621165403842"></p>
<p>看一下有啥images</p>
<p><img src="Docker.assets/image-20200621165733780.png" alt="image-20200621165733780"></p>
<p>启动docker01，用之前建的padaxing/centos 1.0  镜像</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it --name docker01 padaxing/centos:1.0 # 1.0必须写</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200621170429658.png" alt="image-20200621170429658"></p>
<p>当前这个ctrl+p+q不停止退出</p>
<p><img src="Docker.assets/image-20200621170540593.png" alt="image-20200621170540593"></p>
<p>依次启动docker02、docker03</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it --name docker02 --volumes-from docker01 padaxing/centos:1.0</span><br></pre></td></tr></table></figure>

<p>docker02继承docker01的volumes</p>
<p>可以验证，在docker01下加一个数据，在docker02下也会出现</p>
<p><img src="Docker.assets/image-20200621171513650.png" alt="image-20200621171513650"></p>
<p>创建docker03，同样继承docker01</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it --name docker03 --volumes-from docker01 padaxing/centos:1.0</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200621172333639.png" alt="image-20200621172333639"></p>
<p>在docker03的volume01下建立文件，在docker01的volume01下同样也有</p>
<p>即通过–volumes-from 可以实现不同容器间的数据共享</p>
<p>删除docker01，数据还在</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker rm -f </span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200621172830779.png" alt="image-20200621172830779"></p>
<p>可以看到，删除docker01，进入docker02，数据依然在</p>
<p>结论：</p>
<p>容器之间配置信息的传递，数据卷容器的生命周期一直持续到没有容器使用位置</p>
<p>但是如果持久化到了本地，即使所有容器删除了，本地数据是不会删除的</p>
<hr>
<h2 id="DockerFile"><a href="#DockerFile" class="headerlink" title="DockerFile"></a>DockerFile</h2><p>是用来构建docker镜像的文件，可以理解为命令参数脚本</p>
<p>构建步骤：</p>
<ol>
<li>编写一个dockerfile文件</li>
<li>docker build 构建成为一个镜像</li>
<li>docker run运行镜像</li>
<li>docker push 发布镜像（DockerHub、阿里云镜像仓库 私有/共有）</li>
</ol>
<p>这个写一个项目时一样的</p>
<h3 id="官方DockerFile示例"><a href="#官方DockerFile示例" class="headerlink" title="官方DockerFile示例"></a>官方DockerFile示例</h3><p>看一下官方的DockerFile</p>
<p><img src="Docker.assets/image-20200621174204807.png" alt="image-20200621174204807"></p>
<p>可以看到官方镜像都是基础包，很多功能没有，我们通常会自己搭建自己的镜像</p>
<p>官方既然可以制作镜像，我们亦可以</p>
<hr>
<h3 id="DockerFile基础知识"><a href="#DockerFile基础知识" class="headerlink" title="DockerFile基础知识"></a>DockerFile基础知识</h3><ol>
<li>每个指令都必须是大写字母</li>
<li>按照从上到下顺序执行</li>
<li>*#*表示注释</li>
<li>每一个指令都会创建体检一个新的镜像层，并提交</li>
</ol>
<p><img src="Docker.assets/image-20200621174948310.png" alt="image-20200621174948310"></p>
<p>docker是面向开发的，我们以后要发布项目，做镜像，就要编写dockerfile文件，这个文件十分简单！</p>
<p>Docker镜像逐渐成为企业的交付标准，必须掌握！</p>
<hr>
<h3 id="DockerFile命令"><a href="#DockerFile命令" class="headerlink" title="DockerFile命令"></a>DockerFile命令</h3><p><img src="http://www.yunweipai.com/wp-content/uploads/2016/09/171.jpg" alt="æ¥çæºå¾å"></p>
<h3 id="-2"><a href="#-2" class="headerlink" title=""></a></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">FROM # 基础镜像 比如centos</span><br><span class="line">MAINTAINER # 镜像是谁写的 姓名+邮箱</span><br><span class="line">RUN # 镜像构建时需要运行的命令</span><br><span class="line">ADD # 添加，比如添加一个tomcat压缩包</span><br><span class="line">WORKDIR # 镜像的工作目录</span><br><span class="line">VOLUME # 挂载的目录</span><br><span class="line">EXPOSE # 指定暴露端口，跟-p一个道理</span><br><span class="line">RUN # 最终要运行的</span><br><span class="line">CMD # 指定这个容器启动的时候要运行的命令，只有最后一个会生效，而且可被替代</span><br><span class="line">ENTRYPOINT # 指定这个容器启动的时候要运行的命令，可以追加命令</span><br><span class="line">ONBUILD # 当构建一个被继承Dockerfile 这个时候运行ONBUILD指定，触发指令</span><br><span class="line">COPY # 将文件拷贝到镜像中</span><br><span class="line">ENV # 构建的时候设置环境变量</span><br></pre></td></tr></table></figure>

<h3 id="实战构建自己的centos"><a href="#实战构建自己的centos" class="headerlink" title="实战构建自己的centos"></a>实战构建自己的centos</h3><p>Docker Hub中99%的镜像都是从FROM scratch开始的</p>
<p>添加centos7的压缩包</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 创建一个自己的centos</span></span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 进入home目录</span></span><br><span class="line">cd /home</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 创建一个目录，之后的东西都保存到这里</span></span><br><span class="line">mkdir dockerfile</span><br><span class="line"><span class="meta">#</span><span class="bash"> 进入这个目录</span></span><br><span class="line">cd dockerfile/</span><br><span class="line"><span class="meta">#</span><span class="bash"> 创建一个dockerfile，名字叫mydockerfile</span></span><br><span class="line">vim mydockerfile-centos</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>xshell新开一个界面</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 官方默认centos</span></span><br><span class="line">docker run -it centos</span><br><span class="line">pwd # 官方默认有pwd命令</span><br><span class="line">vim # 官方默认没有vim命令</span><br><span class="line">ifconfig # 官方默认没有ifconfig命令</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200621184333206.png" alt="image-20200621184333206"></p>
<p>回到mydockerfile</p>
<p><img src="Docker.assets/image-20200621184847524.png" alt="image-20200621184847524"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 下面给官方centos加上自定义的内容</span></span><br><span class="line">FROM centos</span><br><span class="line">MAINTAINER padaxing&lt;010301200@hai.com&gt;</span><br><span class="line"></span><br><span class="line">ENV MYPATH /usr/local</span><br><span class="line">WORKDIR $MYPATH</span><br><span class="line"></span><br><span class="line">RUN yum -y install vim</span><br><span class="line">RUN yum -y install net-tools</span><br><span class="line"></span><br><span class="line">EXPOSE 80</span><br><span class="line"></span><br><span class="line">CMD echo $MYPATH</span><br><span class="line">CMD echo &quot;---end---&quot;</span><br><span class="line">CMD /bin/bash</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200621185539523.png" alt="image-20200621185539523"></p>
<p>ESC, shif + : 输入wq保存并退出</p>
<p>如果写错了需要修改、</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">vim mydockerfile-centos</span><br><span class="line"><span class="meta">#</span><span class="bash"> 进入之后按i或者INSERT键即可修改</span></span><br></pre></td></tr></table></figure>



<p>下面通过这个这个文件创建镜像</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build -f dockerfile-centos -t mycentos:0.1 .</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200621190219978.png" alt="image-20200621190219978"></p>
<p>依次执行命令</p>
<p><img src="Docker.assets/image-20200621190439448.png" alt="image-20200621190439448"></p>
<p>最终返回Successfully表示成功</p>
<p><img src="Docker.assets/image-20200621190524848.png" alt="image-20200621190524848"></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker run -it mycentos:0.1 # 版本号必须写，不然他会去找最新的</span><br><span class="line">pwd</span><br><span class="line">vim</span><br><span class="line">ifconfig</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200621190812481.png" alt="image-20200621190812481"></p>
<p>这时可以看到这些功能都有了</p>
<p>可以通过查看docker构建历史</p>
<p><img src="Docker.assets/image-20200621192103460.png" alt="image-20200621192103460"></p>
<p>可以看到当前这个镜像是怎么一步一步构建起来的</p>
<p>我们平时拿到一个镜像也可以通过这个方法研究一下他是怎么做的</p>
<h3 id="CMD与ENTRYPOINT"><a href="#CMD与ENTRYPOINT" class="headerlink" title="CMD与ENTRYPOINT"></a>CMD与ENTRYPOINT</h3><p>【视频书签，<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1og4y1q7M4?p=29%EF%BC%8C%E9%9B%A8%E7%BB%88%E4%BA%8E%E5%81%9C%E4%BA%86%EF%BC%8C%E6%83%B3%E5%8E%BB%E6%B1%9F%E8%BE%B9%E8%B7%91%E6%AD%A5%E3%80%91">https://www.bilibili.com/video/BV1og4y1q7M4?p=29，雨终于停了，想去江边跑步】</a></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">FROM centos</span><br><span class="line">CMD [&quot;ls&quot;,&quot;-a&quot;] # 启动centos展示目录</span><br></pre></td></tr></table></figure>

<p><img src="Docker.assets/image-20200622075427103.png" alt="image-20200622075427103"></p>
<p>测试ENTRYPOINT</p>
<p><img src="Docker.assets/image-20200622075653789.png" alt="image-20200622075653789"></p>
<p>run的时候可以直接加命令</p>
<p><img src="Docker.assets/image-20200622075802180.png" alt="image-20200622075802180"></p>
<p>Docker中许多命令都十分相似，我们需要了解他们的区别，最好的方式就是这样对比测试</p>
<h2 id="IDEA整合Docker"><a href="#IDEA整合Docker" class="headerlink" title="IDEA整合Docker"></a>IDEA整合Docker</h2><h2 id="Docker-整合Docker"><a href="#Docker-整合Docker" class="headerlink" title="Docker 整合Docker"></a>Docker 整合Docker</h2><h2 id="Docker-Compose"><a href="#Docker-Compose" class="headerlink" title="Docker Compose"></a>Docker Compose</h2><h2 id="Docker-Swarm"><a href="#Docker-Swarm" class="headerlink" title="Docker Swarm"></a>Docker Swarm</h2><h2 id="各种bug"><a href="#各种bug" class="headerlink" title="各种bug"></a>各种bug</h2><h3 id="Xshell链接失败"><a href="#Xshell链接失败" class="headerlink" title="Xshell链接失败"></a>Xshell链接失败</h3><p><img src="Docker.assets/image-20200616200418271.png" alt="image-20200616200418271"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Connecting to 192.168.147.131:22...</span><br><span class="line">Could not connect to <span class="string">&#x27;192.168.147.131&#x27;</span> (port 22): Connection failed.</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<p>注：电脑休眠重启后，机子会断开，要重新在vmware里面输入ip addr 获得ip</p>
<p><img src="Docker.assets/image-20200616200344918.png" alt="image-20200616200344918"></p>
<p>修改xshell中的主机接口即可</p>
<p>![image-20200616200505065](Docker.assets/image-20200616200505065.png</p>
<p><img src="Docker.assets/image-20200624101454866.png" alt="image-20200624101454866"></p>

    </div>

    
</div>
    <div class="footer" id="footer">
    <p><h4>Copyright © 2020 | Author: 程崎 | Theme By <a class="theme-author" target="_blank" rel="noopener" href="https://github.com/Xunzhuo/hexo-theme-coder" style="font-size:14px; color: #969696">Coder</a></h4>
    <label class="el-switch el-switch-blue el-switch-sm" style="vertical-align: sub;">
        <input type="checkbox" name="switch" id="update_style">
        <span class="el-switch-style"></span>
    </label>
    <!--         <script type="text/javascript">
    var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
    document.write(unescape("%3Cspan id='cnzz_stat_icon_1278548644'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "v1.cnzz.com/stat.php%3Fid%3D1278548644%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
    </script> -->
</p>
</div>
<input type="hidden" id="web_style" value="black">
<input type="hidden" id="valine_appid" value="NOsswOncKgc8HOxqo9oxIWlX-gzGzoHsz">
<input type="hidden" id="valine_appKey" value="z1FihjWEbS8uIfUQdmCtK7zz">

<script src="/blog/libs/jquery.min.js"></script>


<script src="/blog/libs/highlight/highlight.pack.js"></script>

<script src='//cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>

<script src="/blog/js/js.js"></script>

<style type="text/css">
.v * {
color: #698fca;
}
.v .vlist .vcard .vhead .vsys {
color: #3a3e4a;
}
.v .vlist .vcard .vh .vmeta .vat {
color: #638fd5;
}
.v .vlist .vcard .vhead .vnick {
color: #6ba1ff;
}
.v a {
color: #8696b1;
}
.v .vlist .vcard .vhead .vnick:hover {
color: #669bfc;
}
</style>
    <script type="text/javascript" color="173,174,173" opacity='1' zIndex="-2" count="99" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
</body>
</html>
